package com.example.libraryapp.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.libraryapp.R;

/**
 * @author 伍凯铭
 * @since 2020/7/22
 */
public class ArrowView extends FrameLayout {

    private TextView mLeftText;

    private TextView mRightText;

    private ImageView mRightImg;

    private ImageView mRightArrow;

    public ArrowView(@NonNull Context context) {
        this(context,null);
    }

    public ArrowView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArrowView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.arrow_view, this, true);
        mLeftText = view.findViewById(R.id.left_text);
        mRightText = view.findViewById(R.id.right_text);
        mRightImg = view.findViewById(R.id.right_img);
        mRightArrow = view.findViewById(R.id.arrow_img);
        //init
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ArrowView);
        boolean isShowArrow = array.getBoolean(R.styleable.ArrowView_isShowArrow,true);
        boolean isShowRightImg = array.getBoolean(R.styleable.ArrowView_isShowRightImg,false);
        boolean isShowRightTxt = array.getBoolean(R.styleable.ArrowView_isShowRightTxt,true);
        mLeftText.setText(array.getString(R.styleable.ArrowView_leftText));
        if(isShowArrow) {
            showArrow();
        }else {
            hideArrow();
        }
        if(isShowRightTxt) {
            setRightText(array.getString(R.styleable.ArrowView_rightText));
        }else {
            hideRightTxt();
        }
        if(isShowRightImg) {
            showRightImg();
        }else {
            hideRightImg();
        }

        array.recycle();
    }

    public void hideRightImg() {
        mRightImg.setVisibility(GONE);
    }

    public void showRightImg() {
        hideRightTxt();
        hideArrow();
        mRightImg.setVisibility(VISIBLE);
    }


    public void showArrow() {
        mRightArrow.setVisibility(VISIBLE);
    }

    public void hideArrow() {
        mRightArrow.setVisibility(GONE);
    }

    public void hideRightTxt() {
        mRightText.setVisibility(GONE);
    }

    public void setRightText(String text) {
        mRightText.setVisibility(VISIBLE);
        mRightText.setText(text);
    }

    public void setLeftText(String text) {
        mLeftText.setText(text);
    }

}
